﻿#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QDateTime>
#include <QThread>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        QSerialPort serial;
        serial.setPort(info);
        if (serial.open(QIODevice::ReadWrite))
        {
            ui->comboBox->addItem(serial.portName());
            serial.close();
        }
    }
    mb = new Modbus();
    connect(mb, &Modbus::coisValue, this, &Widget::doProCoils);
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::isConnect() const
{
    return mb->isConnect();
}

void Widget::readRequest(int RegisterType, int regAdress, int size, QVector<quint16> *values)
{
    if(isConnect())
    {
        mb->executeReadRequest(RegisterType, regAdress, size, values);
        if(regAdress == 100 && (*values).at(0) == true)
        {
            for (int i = 0; i < (*values).size(); i++)
            {
                //            qDebug() << (*values).at(i);
                ui->textEdit->append(showCurTime() + u8"地址" + QString::number(regAdress + i) + u8":读取值为" + QString::number((*values).at(i)));
            }
        }
    }
}

void Widget::writeRequest(int RegisterType, int regAdress, QVector<quint16> values)
{
    if(isConnect())
    {
//        mb->executeWriteRequest(RegisterType, regAdress,values);
        for(int i = values.size() - 1; i >= 0; --i)
        {
            mb->executeWriteRequest(RegisterType, regAdress + i, QVector<quint16>(1, values.at(i)));
            ui->textEdit->append(showCurTime() + u8"地址" + QString::number(regAdress + i) + u8":写入值为" + QString::number(values.at(i)));
            QThread::msleep(100);
        }
    }
    QVector<quint16> datas;
    readRequest(RegisterType,regAdress,13,&datas);
    for (int i = 0; i < datas.size(); i++)
    {
        //            qDebug() << (*values).at(i);
        ui->textEdit->append(showCurTime() + u8"地址" + QString::number(regAdress + i) + u8":再次读取值为" + QString::number(datas.at(i)));
    }
}

void Widget::timerEvent(QTimerEvent *event)
{
    QVector<quint16> values;
    ModBusType = ui->comboBox_6->currentIndex();
    for (int i = 100; i < 104; i++)
    {
        mb->executeReadRequest(ModBusType, 100, 4, &values);
        if (values.at(0) == true)
        {
            qDebug() << "sucessful!!!";
        }
    }
}

QString Widget::showCurTime()
{
    QDateTime current_time = QDateTime::currentDateTime();
    QString timestr = current_time.toString( QStringLiteral("hh:mm:ss.zzz")); //设置显示的格式
    return QStringLiteral("【") + timestr + QStringLiteral("】");

}

void Widget::on_btnConnect_clicked()
{
    if (ui->btnConnect->text() == QStringLiteral("连接"))
    {
        portName = ui->comboBox->currentText();
        baudRate = ui->comboBox_2->currentText().toInt();
        if (portName.isEmpty()) return;
        mb->connectToSlave(portName, baudRate);
        if (mb->isConnect())
        {
            ui->btnConnect->setText(QStringLiteral("断开连接"));
            ui->comboBox->setEnabled(false);
            ui->comboBox_2->setEnabled(false);
            ui->comboBox_3->setEnabled(false);
            ui->comboBox_4->setEnabled(false);
            ui->comboBox_5->setEnabled(false);
            ui->comboBox_6->setEnabled(false);
            ui->btnRead->setEnabled(true);
            ui->btnSend->setEnabled(true);
            //      startTimer(100);
        }
    }
    else if (ui->btnConnect->text() == QStringLiteral("断开连接"))
    {
        mb->disconnectToSlave();
        ui->btnConnect->setText(QStringLiteral("连接"));
        ui->comboBox->setEnabled(true);
        ui->comboBox_2->setEnabled(true);
        ui->comboBox_3->setEnabled(true);
        ui->comboBox_4->setEnabled(true);
        ui->comboBox_5->setEnabled(true);
        ui->comboBox_6->setEnabled(true);
        ui->btnRead->setEnabled(false);
        ui->btnSend->setEnabled(false);
    }
}

void Widget::doProCoils(bool value)
{
    ui->textEdit->append( u8"线圈" + QString::number(regAdress) + u8":当前值为" + QString::number(value));
    m_sigleCoisValue = value;
}

void Widget::on_btnSend_clicked()
{
    slaveAdress = ui->lineEdit->text().toInt();
    regAdress = ui->lineEdit_2->text().toInt();
    value = ui->textEdit_2->toPlainText().toInt();
    ModBusType = ui->comboBox_6->currentIndex();
    //    mb->executeWriteRequest(slaveAdress,regAdress,Modbus::Coils,value);
    QVector<quint16> values;
    values.push_back(value);
    mb->executeWriteRequest(ModBusType, regAdress, values);
    ui->textEdit->append(showCurTime() + u8"线圈" + QString::number(regAdress) + u8":写入值为" + QString::number(value));
}

void Widget::on_btnRead_clicked()
{
    slaveAdress = ui->lineEdit->text().toInt();
    regAdress = ui->lineEdit_2->text().toInt();
    ModBusType = ui->comboBox_6->currentIndex();
    QVector<quint16> values;
    values.push_back(value);
    mb->executeReadRequest(ModBusType, regAdress, 1, &values);
    for (int i = 0; i < values.size(); i++)
    {
        qDebug() << values.at(i);
        ui->textEdit->append(showCurTime() + u8"线圈" + QString::number(regAdress + i) + u8":读取值为" + QString::number(values.at(i)));
    }
}
